package com.eam.eventhandler;

import com.eam.data.EamFacilitiesCheck;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.dal.service.OBQuery;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.database.SessionInfo;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;

public class FacilityCheckEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EamFacilitiesCheck.ENTITY_NAME) };

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}
	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}
		// 获取项目
		EamFacilitiesCheck details = (EamFacilitiesCheck) event.getTargetInstance();
		// 获取點檢項目ID
		String m_id = details.getEamCheckItem().getId();
		//获取设备ID
		String groupId = details.getEamModelGroup().getId();
		OBQuery<EamFacilitiesCheck> query = OBDal.getInstance().createQuery(EamFacilitiesCheck.class, " as efm where efm.eamCheckItem.id =:m_id and efm.eamModelGroup.id =:groupId");
		query.setNamedParameter("m_id", m_id);
		query.setNamedParameter("groupId", groupId);
		if (query.list().size() > 1) {
			String language = OBContext.getOBContext().getLanguage().getLanguage();
			ConnectionProvider conn = new DalConnectionProvider(false);
			throw new OBException(Utility.messageBD(conn, details.getEamCheckItem().getName() + "已经存在，请仔细检查。", language));
		}
		
	}
	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		String tabId = SessionInfo.getProcessId();
		if (tabId == null) {
			return;
		}
		// 获取项目
		EamFacilitiesCheck details = (EamFacilitiesCheck) event.getTargetInstance();
		//EamFacilitiesCheck old_item = null;
		// 获取點檢項目ID
		String m_id = details.getEamCheckItem().getId();
		//获取设备ID
		String groupId = details.getEamModelGroup().getId();
		OBQuery<EamFacilitiesCheck> query = OBDal.getInstance().createQuery(EamFacilitiesCheck.class, " as efm where efm.eamCheckItem.id =:m_id and efm.eamModelGroup.id =:groupId");
		query.setNamedParameter("m_id", m_id);
		query.setNamedParameter("groupId", groupId);
		if (query.list().size() > 0) {
			String language = OBContext.getOBContext().getLanguage().getLanguage();
			ConnectionProvider conn = new DalConnectionProvider(false);
			throw new OBException(Utility.messageBD(conn, details.getEamCheckItem().getName() + "已经存在，请仔细检查。", language));
		}
		
	}
	public void onDelete(@Observes
	EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
	}
}
